package cn.myzqu.sign.user.server.exception;



import cn.myzqu.sign.common.response.ResultData;
import io.jsonwebtoken.ExpiredJwtException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;


/**
 * 统一异常处理器
 * @Author: 的川
 * @Date: 2018/5/9
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 处理自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public ResultData customExceptionHandler(CustomException e){
        logger.error(e.getMessage(),e);
        return ResultData.error(e.getMessage());
    }

    /**
     * 处理自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(ExpiredJwtException.class)
    public ResultData expiredJwtExceptionHandler(ExpiredJwtException e){
        logger.error(e.getMessage(),e);
        return ResultData.error(991,"Token过期，请重新登录！");
    }


    @ExceptionHandler(SQLException.class)
    public ResultData sqlExceptionHandler(SQLException e){
        logger.error(e.getMessage(),e);
        return ResultData.error("数据库交互异常");
    }

    /**
     * 处理系统异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResultData exceptionHandler(Exception e){
        logger.error(e.getMessage(),e);
        return ResultData.error("请求服务发生异常！");
    }

}
